package com.yc.fresh.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yc.fresh.bean.AdminInfo;
import com.yc.fresh.service.IAdminInfoService;
import com.yc.fresh.util.ConstantKey;
import com.yc.fresh.vo.ResultVO;
/**
 * 后台管理员控制层
 * @author H
 * @date 2021年7月15日
 */
@RestController
@RequestMapping("/admin")
public class AdminInfoController {
	@Autowired
	private IAdminInfoService adminInfoService;
	/**
	 * 处理后端管理员登录
	 * @param admin
	 * @param session
	 * @return
	 */
	@PostMapping("/login")
	public ResultVO login(AdminInfo admin,HttpSession session) {
		AdminInfo adminInfo = adminInfoService.login(admin);
		if(adminInfo == null) {
			return new ResultVO(500,"登录失败");
		}
		session.setAttribute(ConstantKey.CURRENTLOGINADMIN,adminInfo);
		return new ResultVO(200,"成功");
	}
	/**
	 * 检查获取后端登录的管理员信息
	 * @param session
	 * @return
	 */
	@GetMapping("/check")
	public ResultVO check(HttpSession session) {
		Object obj = session.getAttribute(ConstantKey.CURRENTLOGINADMIN);
		if(obj == null) {
			return new ResultVO(501,"未登录");
		}
		
		return new ResultVO(200,"成功",obj);
	}
	
}
